package com.lanyun.controller;

import com.lanyun.pojo.ProductType;
import com.lanyun.pojo.Warehouse;
import com.lanyun.service.ProductTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author 李向南
 * 商品类型
 */
@Controller
public class ProductTypeController {

    @Autowired
    private ProductTypeService pts;

    ProductType product =new ProductType();

    Warehouse warehouse=new Warehouse();

    //查询所有商品类型
    @RequestMapping("/productType")
    public Object showAllWare(Map<String,Object> map){
        List<ProductType> list=pts.allWarehous();
        map.put("allWarehous",list);
        System.out.println(list.size());
        return "productType";
    }


    //增加商品类型
    @RequestMapping("/add_type")
    @ResponseBody
    public  Object addType(HttpServletRequest request){
        System.out.println("=========================================");
       String nameType=request.getParameter("w_name");
        System.out.println(nameType);
       String namew= request.getParameter("w_site");
        System.out.println(namew);
       product.setProtype_name(nameType);
       warehouse.setWarehouse_name(namew);
       product.setWarehouse(warehouse);

        Integer a=pts.add_type(product);


        return a;
    }


    //数据回显
    @RequestMapping(value="/updeat_huixian_type")
    @ResponseBody
    public ProductType updeat_huixian_type(HttpServletRequest request){
        Integer id=Integer.valueOf(request.getParameter("w_ID"));

        ProductType productType= pts.updeat_huixian_type(id);

        return productType;

    }

    //修改仓库
    @RequestMapping(value="/update_type")
    @ResponseBody
    public Object update_type(HttpServletRequest request){
        String name=request.getParameter("u_name");
        String nn=request.getParameter("u_admin");
        Integer id=Integer.valueOf(request.getParameter("id"));
        product.setProtype_name(name);
        product.setProtype_id(id);
        warehouse.setWarehouse_name(nn);
        product.setWarehouse(warehouse);



        Integer a=pts.update_type(product);



        return a;
    }

    //单项删除
    @RequestMapping("/delete_type")
    @ResponseBody
    public Object delete_type(HttpServletRequest request){
        Integer id=Integer.valueOf(request.getParameter("id"));

        warehouse.setWarehouse_id(id);
        product.setWarehouse(warehouse);

        Integer a=pts.delete_type(id);
        return a;
    }

    //批量删除
    @RequestMapping("/deleteType")
    @ResponseBody
    public  Object deleteType(HttpServletRequest request){
        String plid=request.getParameter("ids");
        System.out.println(plid);
        String[] s1=plid.split(",");
        List<Integer> list=new ArrayList<Integer>();
        for (int i = 0; i < s1.length; i++) {
            list.add(Integer.valueOf(s1[i]));
        }

        Integer a=pts.deleteType(list);


        return 1;
    }





}
